package com.hxzy.goods.controller;

import com.changgou.util.Result;
import com.changgou.util.StatusCode;
import com.hxzy.goods.pojo.Brand;
import com.hxzy.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 根据分类名称查询品牌列表
     * @param category
     * @return
     */
    @GetMapping("/category/brand/{category}")
    public Result  findListBrandByCategoryName(@PathVariable String category){
        System.out.println(category);
        List<Map> brandList = brandService.findListByCategoryName(category);
        return new Result(true,StatusCode.OK,"查询成功",brandList);
    }

    /**
     * 根据商品分类名称查询规格列表
     * @param category
     * @return
     */
    @GetMapping("/category/spec/{category}")
    public Result findListSpecByCategoryName(@PathVariable  String category){
        List<Map> specList = brandService.findListSpecByCategoryName(category);
        return new Result(true,StatusCode.OK,"",specList);
    }

    @GetMapping("/findAll")
    public Result<List<Brand>> findAll(){
        List<Brand> brandList = brandService.findAll();
        return new Result<List<Brand>>(true, StatusCode.OK, "查询成功", brandList);
    }

    @GetMapping("/findList")
    public Result<List<Brand>> findList(@RequestParam Map searchMap){
        List<Brand> brandList = brandService.findBrandBySelector(searchMap);
        return new Result<List<Brand>>(true, StatusCode.OK, "查询成功", brandList);
    }

    @GetMapping("/findById/{id}")
    public Result<Brand> findById(@PathVariable("id")  Integer id){
        Brand brand = brandService.findById(id);
        return new Result<Brand>(true, StatusCode.OK, "查询成功", brand);
    }

    @PostMapping("/update")
    public Result update(@RequestBody Brand brand){
        int number = brandService.updateBrand(brand);
        String msg = number > 0 ? "更新成功": "更新失败";
        return new Result(true, StatusCode.OK, msg, number);
    }

    @GetMapping("/delete/{id}")
    public Result delete(@PathVariable("id")  Integer id){
        int number = brandService.deleteById(id);
        String msg = number > 0 ? "删除成功": "删除失败";
        return new Result(true, StatusCode.OK, msg, number);
    }

    @PostMapping("/add")
    public Result addBrand(@RequestBody Brand brand){
        int number = brandService.addBrand(brand);
        String msg = number > 0 ? "添加成功": "添加失败";
        return new Result(true, StatusCode.OK, msg, number);
    }

}
